package main

import (
	"database/sql"
	"fmt"
	"github.com/gin-gonic/gin"
	_ "github.com/go-sql-driver/mysql"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	"go-demo/controller"
	"go-demo/docs"
	"go-demo/domain"
	"go-demo/service"
	"net/http"
)

func main() {
	var db, err = sql.Open("mysql", "root:123456@tcp(localhost:3306)/vehicle?parseTime=true&loc=Local")
	if err != nil {
		fmt.Printf(err.Error())
		return
	}

	var demoRepository = domain.NewDemoRepository(db)
	var auditLogRepository = domain.NewAuditLogRepository(db)
	var uow = domain.NewUnitOfWork(db)
	var demoAppService = service.NewDemoAppService(uow, demoRepository)
	var auditLogAppService = service.NewAuditLogAppService(uow, auditLogRepository)
	var demoController = controller.NewDemoController(demoAppService)
	var auditLogController = controller.NewAuditLogController(auditLogAppService)

	var r = gin.Default()

	//r.Use(common.JsonResponseWriter())
	v1Group := r.Group("/v1")
	{
		//订单管理
		demoGroup := v1Group.Group("/Demo")
		{
			demoGroup.GET("", demoController.GetList)
			demoGroup.GET(":id", demoController.GetOne)
			demoGroup.POST("", demoController.Create)
			demoGroup.DELETE("", demoController.Delete)
		}
		//审计日志
		auditLogGroup := v1Group.Group("/AuditLog")
		{
			auditLogGroup.GET("", auditLogController.GetList)
			auditLogGroup.GET(":id", auditLogController.GetOne)
		}
	}

	//swagger
	docs.SwaggerInfo.BasePath = "/v1"
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	r.GET("/", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/swagger/index.html") })

	fmt.Printf("访问地址(WEBAPI):http://localhost:8080 \n")
	r.Run(":8080")
}
